home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / textual / pdftops / xpdf / h / GfxState < prev    next >
Text File  |  1996-06-08  |  10KB  |  342 lines

  1. //========================================================================
  2. //
  3. // GfxState.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifndef GFXSTATE_H
  10. #define GFXSTATE_H
  11.  
  12. #ifdef __GNUC__
  13. //#pragma interface
  14. #endif
  15.  
  16. #include "gtypes.h"
  17.  
  18. class Object;
  19. class GfxFont;
  20.  
  21. //------------------------------------------------------------------------
  22. // GfxColor
  23. //------------------------------------------------------------------------
  24.  
  25. class GfxColor {
  26. public:
  27.  
  28.   GfxColor(): r(0), g(0), b(0) {}
  29.  
  30.   // Set color.
  31.   void setGray(double gray)
  32.     { r = g = b = gray; }
  33.   void setCMYK(double c, double m, double y, double k);
  34.   void setRGB(double r1, double g1, double b1)
  35.     { r = r1; g = g1; b = b1; }
  36.  
  37.   // Accessors.
  38.   double getR() { return r; }
  39.   double getG() { return g; }
  40.   double getB() { return b; }
  41.   double getGray() { return 0.299 * r + 0.587 * g + 0.114 * b; }
  42.  
  43. private:
  44.  
  45.   double r, g, b;
  46. };
  47.  
  48. //------------------------------------------------------------------------
  49. // GfxColorSpace
  50. //------------------------------------------------------------------------
  51.  
  52. enum GfxColorMode {
  53.   colorGray, colorCMYK, colorRGB
  54. };
  55.  
  56. class GfxColorSpace {
  57. public:
  58.  
  59.   // Constructor.
  60.   GfxColorSpace(int bits1, Object *colorSpace, Object *decode);
  61.  
  62.   // Destructor.
  63.   ~GfxColorSpace();
  64.  
  65.   // Is color space valid?
  66.   GBool isOk() { return ok; }
  67.  
  68.   // Get stream decoding info.
  69.   int getNumComponents() { return numComponents; }
  70.   int getBits() { return bits; }
  71.  
  72.   // Get color mode.
  73.   GBool isIndexed() { return indexed; }
  74.  
  75.   // Convert an input value to a color.
  76.   void getRGB(Guchar x[4], Guchar *r, Guchar *g, Guchar *b);
  77.   void getGray(Guchar x[4], Guchar *gray);
  78.  
  79. private:
  80.  
  81.   GfxColorMode mode;        // color mode
  82.   GBool indexed;        // use lookup table?
  83.   int bits;            // bits per component
  84.   int numComponents;        // number of components in input values
  85.   int lookupComponents;        // number of components in lookup table
  86.   Guchar (*lookup)[4];        // lookup table
  87.   GBool ok;            // is color space valid?
  88. };
  89.  
  90. //------------------------------------------------------------------------
  91. // GfxSubpath and GfxPath
  92. //------------------------------------------------------------------------
  93.  
  94. class GfxSubpath {
  95. public:
  96.  
  97.   // Constructor.
  98.   GfxSubpath(double x1, double y1);
  99.  
  100.   // Destructor.
  101.   ~GfxSubpath();
  102.  
  103.   // Copy.
  104.   GfxSubpath *copy() { return new GfxSubpath(x, y, n, size); }
  105.  
  106.   // Get points.
  107.   int getNumPoints() { return n; }
  108.   double getX(int i) { return x[i]; }
  109.   double getY(int i) { return y[i]; }
  110.  
  111.   // Get last point.
  112.   double getLastX() { return x[n-1]; }
  113.   double getLastY() { return y[n-1]; }
  114.  
  115.   // Add a segment.
  116.   void lineTo(double x1, double y1);
  117.  
  118.   // Close the subpath.
  119.   void close()
  120.     { if (x[n-1] != x[0] || y[n-1] != y[0]) lineTo(x[0], y[0]); }
  121.  
  122. private:
  123.  
  124.   double *x, *y;        // points
  125.   int n;            // number of points
  126.   int size;            // size of x/y arrays
  127.  
  128.   GfxSubpath(double *x1, double *y1, int n1, int size1);
  129. };
  130.  
  131. class GfxPath {
  132. public:
  133.  
  134.   // Constructor.
  135.   GfxPath();
  136.  
  137.   // Destructor.
  138.   ~GfxPath();
  139.  
  140.   // Copy.
  141.   GfxPath *copy() { return new GfxPath(subpaths, n, size); }
  142.  
  143.   // Is the path non-empty, i.e., is there a current point?
  144.   GBool isPath() { return n > 0; }
  145.  
  146.   // Get subpaths.
  147.   int getNumSubpaths() { return n; }
  148.   GfxSubpath *getSubpath(int i) { return subpaths[i]; }
  149.  
  150.   // Get last point on last subpath.
  151.   double getLastX() { return subpaths[n-1]->getLastX(); }
  152.   double getLastY() { return subpaths[n-1]->getLastY(); }
  153.  
  154.   // Move the current point, i.e., start a new subpath.
  155.   void moveTo(double x, double y);
  156.  
  157.   // Add a segment to the last subpath.
  158.   void lineTo(double x, double y) { subpaths[n-1]->lineTo(x, y); }
  159.  
  160.   // Close the last subpath.
  161.   void close() { subpaths[n-1]->close(); }
  162.  
  163. private:
  164.  
  165.   GfxSubpath **subpaths;    // subpaths
  166.   int n;            // number of subpaths
  167.   int size;            // size of subpaths array
  168.  
  169.   GfxPath(GfxSubpath **subpaths1, int n1, int size1);
  170. };
  171.  
  172. //------------------------------------------------------------------------
  173. // GfxState
  174. //------------------------------------------------------------------------
  175.  
  176. class GfxState {
  177. public:
  178.  
  179.   // Construct a default GfxState, for a device with resolution <dpi>,
  180.   // page box (<x1>,<y1>)-(<x2>,<y2>), page rotation <rotate>, and
  181.   // coordinate system specified by <upsideDown>.
  182.   GfxState(int dpi, int x1, int y1, int x2, int y2, int rotate,
  183.        GBool upsideDown);
  184.  
  185.   // Destructor.
  186.   ~GfxState();
  187.  
  188.   // Copy.
  189.   GfxState *copy() { return new GfxState(this); }
  190.  
  191.   // Accessors.
  192.   double *getCTM() { return ctm; }
  193.   int getPageWidth() { return pageWidth; }
  194.   int getPageHeight() { return pageHeight; }
  195.   GfxColor *getFillColor() { return &fillColor; }
  196.   GfxColor *getStrokeColor() { return &strokeColor; }
  197.   double getLineWidth() { return lineWidth; }
  198.   void getLineDash(double **dash, int *length, double *start)
  199.     { *dash = lineDash; *length = lineDashLength; *start = lineDashStart; }
  200.   int getFlatness() { return flatness; }
  201.   int getLineJoin() { return lineJoin; }
  202.   int getLineCap() { return lineCap; }
  203.   double getMiterLimit() { return miterLimit; }
  204.   GfxFont *getFont() { return font; }
  205.   double getFontSize() { return fontSize; }
  206.   double *getTextMat() { return textMat; }
  207.   double getCharSpace() { return charSpace; }
  208.   double getWordSpace() { return wordSpace; }
  209.   double getHorizScaling() { return horizScaling; }
  210.   double getLeading() { return leading; }
  211.   double getRise() { return rise; }
  212.   int getRender() { return render; }
  213.   GfxPath *getPath() { return path; }
  214.   double getCurX() { return curX; }
  215.   double getCurY() { return curY; }
  216.   double getLineX() { return lineX; }
  217.   double getLineY() { return lineY; }
  218.  
  219.   // Is there a current point?
  220.   GBool isPath() { return path->isPath(); }
  221.  
  222.   // Transforms.
  223.   void transform(double x1, double y1, double *x2, double *y2)
  224.     { *x2 = ctm[0] * x1 + ctm[2] * y1 + ctm[4];
  225.       *y2 = ctm[1] * x1 + ctm[3] * y1 + ctm[5]; }
  226.   void transformDelta(double x1, double y1, double *x2, double *y2)
  227.     { *x2 = ctm[0] * x1 + ctm[2] * y1;
  228.       *y2 = ctm[1] * x1 + ctm[3] * y1; }
  229.   void textTransform(double x1, double y1, double *x2, double *y2)
  230.     { *x2 = textMat[0] * x1 + textMat[2] * y1 + textMat[4];
  231.       *y2 = textMat[1] * x1 + textMat[3] * y1 + textMat[5]; }
  232.   void textTransformDelta(double x1, double y1, double *x2, double *y2)
  233.     { *x2 = textMat[0] * x1 + textMat[2] * y1;
  234.       *y2 = textMat[1] * x1 + textMat[3] * y1; }
  235.   double transformWidth(double w);
  236.   double getTransformedLineWidth()
  237.     { return transformWidth(lineWidth); }
  238.   double getTransformedFontSize();
  239.   void getFontTransMat(double *m11, double *m12, double *m21, double *m22);
  240.  
  241.   // Change state parameters.
  242.   void concatCTM(double a, double b, double c,
  243.          double d, double e, double f);
  244.   void setFillGray(double gray)
  245.     { fillColor.setGray(gray); }
  246.   void setFillCMYK(double c, double m, double y, double k)
  247.     { fillColor.setCMYK(c, m, y, k); }
  248.   void setFillRGB(double r, double g, double b)
  249.     { fillColor.setRGB(r, g, b); }
  250.   void setStrokeGray(double gray)
  251.     { strokeColor.setGray(gray); }
  252.   void setStrokeCMYK(double c, double m, double y, double k)
  253.     { strokeColor.setCMYK(c, m, y, k); }
  254.   void setStrokeRGB(double r, double g, double b)
  255.     { strokeColor.setRGB(r, g, b); }
  256.   void setLineWidth(double width)
  257.     { lineWidth = width; }
  258.   void setLineDash(double *dash, int length, double start);
  259.   void setFlatness(int flatness1) { flatness = flatness1; }
  260.   void setLineJoin(int lineJoin1) { lineJoin = lineJoin1; }
  261.   void setLineCap(int lineCap1) { lineCap = lineCap1; }
  262.   void setMiterLimit(double miterLimit1) { miterLimit = miterLimit1; }
  263.   void setFont(GfxFont *font1, double fontSize1)
  264.     { font = font1; fontSize = fontSize1; }
  265.   void setTextMat(double a, double b, double c,
  266.           double d, double e, double f)
  267.     { textMat[0] = a; textMat[1] = b; textMat[2] = c;
  268.       textMat[3] = d; textMat[4] = e; textMat[5] = f; }
  269.   void setCharSpace(double space)
  270.     { charSpace = space; }
  271.   void setWordSpace(double space)
  272.     { wordSpace = space; }
  273.   void setHorizScaling(double scale)
  274.     { horizScaling = scale; }
  275.   void setLeading(double leading1)
  276.     { leading = leading1; }
  277.   void setRise(double rise1)
  278.     { rise = rise1; }
  279.   void setRender(int render1)
  280.     { render = render1; }
  281.  
  282.   // Add to path.
  283.   void moveTo(double x, double y)
  284.     { path->moveTo(curX = x, curY = y); }
  285.   void lineTo(double x, double y)
  286.     { path->lineTo(curX = x, curY = y); }
  287.   void curveTo(double x1, double y1, double x2, double y2,
  288.            double x3, double y3);
  289.   void closePath()
  290.     { path->close(); curX = path->getLastX(); curY = path->getLastY(); }
  291.   void clearPath();
  292.  
  293.   // Text position.
  294.   void textMoveTo(double tx, double ty)
  295.     { lineX = tx; lineY = ty; textTransform(tx, ty, &curX, &curY); }
  296.   void textShift(double tx);
  297.  
  298.   // Push/pop GfxState on/off stack.
  299.   GfxState *save();
  300.   GfxState *restore();
  301.  
  302. private:
  303.  
  304.   double ctm[6];        // coord transform matrix
  305.   int pageWidth, pageHeight;    // page size (pixels)
  306.  
  307.   GfxColor fillColor;        // fill color
  308.   GfxColor strokeColor;        // stroke color
  309.  
  310.   double lineWidth;        // line width
  311.   double *lineDash;        // line dash
  312.   int lineDashLength;
  313.   double lineDashStart;
  314.   int flatness;            // curve flatness
  315.   int lineJoin;            // line join style
  316.   int lineCap;            // line cap style
  317.   double miterLimit;        // line miter limit
  318.  
  319.   GfxFont *font;        // font
  320.   double fontSize;        // font size
  321.   double textMat[6];        // text matrix
  322.   double charSpace;        // character spacing
  323.   double wordSpace;        // word spacing
  324.   double horizScaling;        // horizontal scaling
  325.   double leading;        // text leading
  326.   double rise;            // text rise
  327.   int render;            // text rendering mode
  328.  
  329.   GfxPath *path;        // array of path elements
  330.   double curX, curY;        // current point (user coords)
  331.   double lineX, lineY;        // start of current text line (text coords)
  332.  
  333.   GfxState *saved;        // next GfxState on stack
  334.  
  335.   GfxState(GfxState *state);
  336.   void doCurveTo(double x0, double y0, double x1, double y1,
  337.          double x2, double y2, double x3, double y3,
  338.          int splits);
  339. };
  340.  
  341. #endif
  342.